home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / newmarch.zip / FONTINFO.C < prev    next >
C/C++ Source or Header  |  1992-09-08  |  6KB  |  239 lines

  1. /* Author:   $Author: jan $
  2.  * File:     $Source: /usr/usrs/jan/desktop/X_Book.boo/programs/RCS/fontinfo.c,v $
  3.  * Date:     $Date: 1992/09/09 00:09:53 $
  4.  * Revision: $Revision: 1.1 $
  5.  */
  6.  
  7. #include "copyright.h"
  8.  
  9. /*     
  10. ** File: onewindow.c
  11. ** Generic program schema for a one window program    
  12. */ 
  13.     
  14. #include <stdio.h>    
  15. #include <X11/Xlib.h>    
  16. #include <X11/Xutil.h>    
  17.     
  18. /*    
  19. ** debug lists all events to an 
  20. ** xterm window as they occur
  21. */    
  22. #define DEBUG    
  23.     
  24. char WINDOW_NAME[] = "Window";    
  25. char ICON_NAME[] = "Icon";    
  26.     
  27. Display *display;  /* the display device */    
  28. int     screen;    /* the screen on the display */    
  29. Window  main_window;    
  30. GC    gc;    
  31. unsigned long  foreground, background;    
  32.  
  33. char string[] = "hello world";
  34.  
  35. void
  36. fontinfo ()
  37. {
  38.     int direction, ascent, descent;
  39.     int height, width;
  40.     XCharStruct overall;
  41.     GContext font;    
  42.  
  43.     font = XGContextFromGC(gc);
  44.     XQueryTextExtents (display, font,
  45.             string, strlen (string),
  46.             &direction, &ascent,
  47.             &descent,
  48.             &overall);
  49.     height = ascent + descent;
  50.     width = overall.width;
  51.  
  52.     fprintf (stderr, "height : %d, width: %d\n",
  53.          height, width);
  54. }
  55.  
  56. /*    
  57. ** 
  58. */    
  59.     
  60. Window    
  61. openWindow (x, y, width, height, border_width,
  62.             argc, argv)    
  63.     int x, y;  /* coords of the upper left 
  64.                   corner in pixels */
  65.     int width,    
  66.         height;  /* size of the window in pixels */    
  67.     int border_width;  /* the border width is 
  68.                           not included in the    
  69.                           other dimensions */    
  70.     int argc;    
  71.     char **argv;    
  72. {    
  73.     Window  new_window;    
  74.     XSizeHints  size_hints;    
  75.         
  76.     /* now create the window */    
  77.     new_window = XCreateSimpleWindow (display,     
  78.                         DefaultRootWindow (display),    
  79.                         x, y, width, height,    
  80.                         border_width,    
  81.                         foreground, background);    
  82.             
  83.     /* set up the size hints for the window manager */
  84.     size_hints.x = x;    
  85.     size_hints.y = y;    
  86.     size_hints.width = width;    
  87.     size_hints.height = height;    
  88.     /* and state what hints are included */    
  89.     size_hints.flags = PPosition | PSize;    
  90.     
  91.     /* let the window manager know about the window */
  92.     XSetStandardProperties (display, new_window,    
  93.           WINDOW_NAME, ICON_NAME,    
  94.           None,      /* no icon map */    
  95.           argv, argc, &size_hints);    
  96.     
  97.     /* Decide what events the window will receive */
  98.     XSelectInput (display, new_window,    
  99.           (ButtonPressMask | KeyPressMask | 
  100.            ExposureMask));    
  101.     
  102.     /* Return the window ID */    
  103.     return (new_window);    
  104. }    
  105.     
  106. /*    
  107. ** 
  108. */    
  109. GC
  110. getGC ()    
  111. {   GC gc;
  112.     XGCValues gcValues;    
  113.     
  114.     gc = XCreateGC (display, main_window,     
  115.                 (unsigned long) 0, &gcValues);    
  116.     
  117.     XSetBackground (display, gc, background);    
  118.     XSetForeground (display, gc, foreground);    
  119.  
  120.     return (gc);
  121. }    
  122.     
  123. /*    
  124. ** Terminate the program gracefully    
  125. */    
  126. quitX ()    
  127. {    
  128.     XCloseDisplay (display);    
  129.     exit (0);    
  130. }    
  131.     
  132.     
  133. /* 
  134. ** An expose event occurs when the contents of
  135. ** a window are invalidated and at least some
  136. ** of it needs to be redrawn 
  137. */ 
  138. void    
  139. doExposeEvent (pEvent)    
  140.     XExposeEvent *pEvent;    
  141. {    
  142.     XDrawImageString (display, main_window, gc,    
  143.           10, 10, "Press q or any button to quit",    
  144.           strlen ("Press q or any button to quit"));    
  145. }    
  146.     
  147. /* 
  148. ** A button has been pressed within the window - quit
  149. */ 
  150. void doButtonPressEvent (pEvent)    
  151.     XButtonEvent *pEvent;    
  152. {    
  153.     quitX ();    
  154. }    
  155.     
  156. /* 
  157. ** A key has been pressed. 
  158. ** It needs to be decoded and acted on.
  159. ** Quit if it is a 'q'.
  160. */ 
  161. void    
  162. doKeyPressEvent (pEvent)    
  163.     XKeyEvent *pEvent;    
  164. {   int key_buffer_size = 10;    
  165.     char key_buffer[9];    
  166.     XComposeStatus compose_status;    
  167.     KeySym key_sym;    
  168.     
  169.     XLookupString (pEvent, key_buffer, 
  170.           key_buffer_size,    
  171.           &key_sym, &compose_status);    
  172.     if (key_buffer[0] == 'q')    
  173.           quitX ();    
  174. }    
  175.     
  176. void
  177. initX ()    
  178. {    
  179.     /* set the display name from the 
  180.     ** environment vbl DISPLAY */    
  181.     display = XOpenDisplay (NULL);    
  182.     if  (display == NULL)    
  183.     {      fprintf (stderr, 
  184.                     "Unable to open display %s\n",    
  185.                     XDisplayName (NULL));    
  186.           exit (1);    
  187.     }    
  188.     screen = DefaultScreen (display);    
  189.     /* use the default foreground and 
  190.     ** background colors 
  191.     */    
  192.     foreground = BlackPixel (display, screen);    
  193.     background = WhitePixel (display, screen);    
  194.     
  195. }    
  196.     
  197. main (argc, argv)    
  198.     int argc;    
  199.     char **argv;    
  200. {   XEvent event;    
  201.     
  202.     initX ();    
  203.     
  204.     main_window = openWindow (10, 20, 200, 100, 5, 
  205.                               argc, argv);    
  206.     gc = getGC ();    
  207.     
  208.     fontinfo();
  209.  
  210.     /* Display the window on the screen */    
  211.     XMapWindow (display, main_window);    
  212.     XFlush (display);    
  213.     while (True)    
  214.     {    
  215.           XNextEvent (display, &event);    
  216. #ifdef DEBUG    
  217.           printf ("Event number is %d\n", event.type);
  218. #endif    
  219.           switch  (event.type)    
  220.           {    
  221.           case Expose:  
  222.                       doExposeEvent (&event);    
  223.                       break;    
  224.     
  225.           case ButtonPress:    
  226.                       doButtonPressEvent (&event);    
  227.                       break;    
  228.     
  229.           case KeyPress:
  230.                       doKeyPressEvent (&event);    
  231.                       break;    
  232.     
  233.           case MappingNotify:    
  234.                       XRefreshKeyboardMapping (&event);
  235.                       break;    
  236.           }    
  237.     }    
  238. }
  239.